define(function (require) {
	var zsApp = require('app');  
	zsApp.controller('classificationCtrl', ['$rootScope', '$scope', '$http', '$timeout', 'cache', 'selectCache', 'promptCache', function($rootScope, $scope, $http, $timeout, cache, selectCache, promptCache) {

		// 上级机构ID
	    $scope.parentId = cache.get('parentId');
	    // 上级资源分类名
	    $scope.productName = cache.get('productName');
	    
	    // pagination
	    $rootScope.paginationConf = { 
	        currentPage    : 1,
	        totalItems     : 10,
	        itemsPerPage   : 10,
	        pagesLength    : 15,
	        perPageOptions : [20, 30, 40, 50, 60],
	        onChange: function(){ 
	            var self = this,
	                treeReqVo = {
	                    id          : cache.get('productCategoryId')||0,
	                    pageNumber  : self.currentPage,  
	                    pageSize    : self.itemsPerPage,  
	                };
	            $http
	            ({
	                url    : $scope.zsUrl+'digitalhome-boss/product/proCategors/getSublevelInfo',
	                method : 'POST',
	                data   : treeReqVo,

	            }).success(function(res) {

	                if( res.result_code != '0000' ) {
	                    $scope.isNoData = false;
	                    return false;
	                }
	                    
	                // 表格数据
	                $scope.tabVal = res.result;
	                // 遍历每条数据对应的ID
	                angular.forEach(res.result, function(obj, i) {
	                    $scope.dataID[i] = obj.productCategoryId;
	                });
	                // 总共有多少条数据
	                self.totalItems = res.length;

	            }).error(function(err) {
	                $scope.isNoData = false;
	            });

	        }
	    };

	    // reload table
	    $scope.reloadPagination = function() {
	        $scope.paginationConf.onChange();
	    };

	    // newlyAdded modal
	    $scope.newlyAdded = function() {
	        superior();
	    };
	    // newlyAdded submit
	    $scope.submitNewlyAdded = function() {

	        var newlyAddedData = {
	            parentId         : cache.get('productCategoryId'),
	            productCategoryId    : '',
	            productName          : $scope.newlyAdded_productName||'',
	        	categoryOrder    : $scope.newlyAdded_categoryOrder||'',
	        	description      : $scope.newlyAdded_description||''          
	        }

	        $http
	        ({
	            url         : $scope.zsUrl+'digitalhome-boss/product/proCategors/addProCategors',
	            method      : 'POST',
	            data        : newlyAddedData,

	        }).success(function(res) {

	            if( res.result_code != '0000' ) {
	                promptCache.errorPrompt(res.message);
	                return false;
	            }

	            promptCache.successPrompt(res.message);
	            reloadTable();
	            
	        }).error(function(err) {
	            promptCache.errorPrompt('请求失败！');
	            return false;
	        });
	    };

	    // details
	    $scope.details = function(i) { 
	        
	        superior();

	        $http
	        ({
	            url     : $scope.zsUrl+'digitalhome-boss/product/proCategors/getProCategors/'+$scope.dataID[i],
	            method  : 'GET',

	        }).success(function(res) {

	            if( res.result_code != '0000' ) {
	                return false;
	            }

	            $scope.details_productName      = res.result.productName;
	            $scope.details_categoryOrder    = res.result.categoryOrder;
	            $scope.details_description      = res.result.description;  
	        }).error(function(err) {
	            promptCache.errorPrompt('请求失败！');
	            return false;
	        });         
	    };

	    // edit: show data
	    $scope.edit = function(i) {

	        superior();

	        $scope.index = i;
	        $http
	        ({
	            url     : $scope.zsUrl+'digitalhome-boss/product/proCategors/getProCategors/'+$scope.dataID[i],
	            method  : 'GET',

	        }).success(function(res) {

	            if( res.result_code != '0000' ) {
	                return false;
	            }

	            $scope.edit_parentId         = res.result.parentId;
	            $scope.edit_productName      = res.result.productName;
	            $scope.edit_categoryOrder    = res.result.categoryOrder;
	            $scope.edit_description      = res.result.description;
	        }).error(function(err) {
	            promptCache.errorPrompt('请求失败！');
	            return false;
	        });
	    };

	    // edit: submit data
	    $scope.submitEdit = function() {
	        var editData = {
	            parentId                : $scope.edit_parentId||'',
	            productCategoryId       : $scope.dataID[$scope.index]||'',
	            productName             : $scope.edit_productName||'',
	            categoryOrder           : $scope.edit_categoryOrder,
	            description             : $scope.edit_description||''          
	        }

	        $http
	        ({
	            url     : $scope.zsUrl+'digitalhome-boss/product/proCategors/modify',
	            method  : 'POST',
	            data    : editData,

	        }).success(function(res) {

	            if( res.result_code != '0000' ) {
	                promptCache.errorPrompt(res.message);
	                return false;
	            }

	            promptCache.successPrompt(res.message);
	            reloadTable();

	        }).error(function(err) {
	            promptCache.errorPrompt('修改失败！');
	        });
	    };

	    // delete enable disabled
	    $scope.getDataID = function() {
	        // data length
	        var len = $scope.dataID.length;
	        // param id
	        var whichId = 'productCategoryId';
	        // param
	        var deleteUrl = 'product/proCategors/delete';
	        // enable disabled
//	        var isEnableUrl = 'product/resCategors/modifyStatus';

	        // multiple selections 
	        $scope.selectAll= function (bool, obj) {
	            selectCache.selectAll(bool, $scope.dataID);
	        };

	        //  Single or multiple selections 
	        $scope.select= function (selectObj, bool) {   
	            var selectId = selectObj.productCategoryId;        
	            selectCache.select(selectId, bool, len);    
	        }; 

	        // check
	        $scope.check= function () {
	            selectCache.check();
	        };

	        // delete
	        $scope.delete= function () { 
	            selectCache.delete(whichId, deleteUrl);
	            reloadTable();
	        };
	    };

	    // superior
	    function superior() {
	        if( cache.get('productName') ) {
	            $scope.productName = cache.get('productName');
	        }else {
	            $scope.productName = '最上级';
	        }
	    };
	    // reload table
	    function reloadTable() {
	        $timeout(function() {
	            $scope.paginationConf.onChange();
	            $scope.master = false;
	        },1500);
	    };
	}])
	.directive('classificationNewlyadded', function() {
		return {
			restrict	: 'E',
			templateUrl : 'goods/classification/newly_added.html',
			replace		: true,	
		}
	})
	.directive('classificationDetails', function() {
		return {
			restrict	: 'E',
			templateUrl	: 'goods/classification/details.html',
			replace		: true,	
		}
	})
	.directive('classificationEdit', function() {
		return {
			restrict	: 'E',
			templateUrl	: 'goods/classification/edit.html',
			replace		: true,	
		}
	})
	.directive('classificationDelete', function() {
		return {
			restrict	: 'E',
			templateUrl	: 'goods/classification/delete.html',
			replace		: true,	
		}
	})
	.directive('classificationTable', function() {
	    return {
	        restrict    : 'E',
	        templateUrl : 'goods/classification/table.html',
	        replace     : true, 
	    }
	})
	});
